package mes.kanban.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import mes.kanban.entity.MesPermission;
import mes.kanban.respository.UserPermissionRespository;
import mes.kanban.service.MesPermissionServiceI;
import mes.kanban.util.PageBean;

/**
 * 权限操作类
 * @author gjy
 *
 * 2018年2月13日下午4:33:12
 */
@Service
public class MesPermissionServiceImpl implements MesPermissionServiceI {
	
	@Autowired
	private UserPermissionRespository permissionRespository;

	@Override
	public List<MesPermission> getPermissions() {
		return permissionRespository.findAll();
	}
	
	@Override
	public PageBean getPermissionsForBean(Integer pageNum, Integer pageSize) {
		
		PageBean pb = new PageBean();
		
		Pageable pa = new PageRequest(pageNum-1, pageSize);
		
		Page<MesPermission> permisssions = this.permissionRespository.findAll(pa);
		
		pb.setCurrentPage(pageNum);
		pb.setPageSize(pageSize);
		pb.setList(permisssions.getContent());
		pb.setAllCounts((int) permisssions.getTotalElements());
		pb.setTotalPage(permisssions.getTotalPages());
		
		return pb;
	}

	@Override
	public void deleteById(String id) {
		this.permissionRespository.delete(id);
	}

	@Override
	public void savePermission(MesPermission permission) {
		this.permissionRespository.save(permission);
	}

}
